//
//  GoCashApp.swift
//  GoCash
//
//  Created by jie on 2024/4/30.
//

import SwiftUI

@main
struct GoCashApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    @Environment(\.scenePhase) private var scenePhase
    
    @ObservedObject private var appState = AppState.shared
    
    var body: some Scene {
        WindowGroup {
            ZStack {
                switch appState.mainPageState {
                    case 0: LuanchPage().environmentObject(appState)
                    case 1: HomePage()
                    case 2: LoginView()
                    default: ContentView()
                }
                
                ///显示弹窗
                if appState.showAlert == true {
                    GeometryReader(content: { geometry in
                        VStack {
                            Spacer()
                            appState.alertView
                            Spacer()
                        }
                    })
                    .background(Color.black.opacity(0.52).edgesIgnoringSafeArea(.all).onTapGesture {
                        withAnimation {
                            appState.showAlert = false
                        }
                    })
                }
                
                if appState.showLoading == true {
                    GeometryReader(content: { geometry in
                        VStack {
                            Spacer()
                            Text("Loading")
                            Spacer()
                        }
                    })
                    .background(Color.black.opacity(0.2).edgesIgnoringSafeArea(.all))
                }
            }
        }
//        .onChange(of: scenePhase) { oldScenePhase, newScenePhase in
//            switch newScenePhase {
//            case .active:
//                print("应用启动了")
//            case .background:
//                print("应用在后台展示")
//            case .inactive:
//                print("应用休眠了")
//                
//            @unknown default:
//                print("default")
//            }
//        }
    }
}


let APP_NAME = "Go Cash"


class AlertViewManager: ObservableObject{
    static var shared = AlertViewManager()
    
}


class AppState: ObservableObject {
    
    static var shared = AppState()
    
    ///0: 启动；
    ///1: Home:
    ///2: 登陆;
    @Published var mainPageState: Int = 1
    
    @Published var showAlert: Bool = false
    var alertView: AnyView?
    
    @Published var showLoading: Bool = false
    var loading: AnyView?
}
